package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.OrderInvoiceBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 查看订单发票信息
 *
 * @author fengzhen
 * @version v1.0, 2018/4/13
 */
public class InvoiceDetailActivity extends BasePersonActivity {

    @BindView(R.id.tv_status_activity_invoice_detail)
    TextView mTvStatusActivityInvoiceDetail;
    @BindView(R.id.tv_orderno_activity_invoice_detail)
    TextView mTvOrdernoActivityInvoiceDetail;
    @BindView(R.id.tv_date_activity_invoice_detail)
    TextView mTvDateActivityInvoiceDetail;
    @BindView(R.id.tv_type_activity_invoice_detail)
    TextView mTvTypeActivityInvoiceDetail;
    @BindView(R.id.linear_type_activity_invoice_detail)
    LinearLayout mLinearTypeActivityInvoiceDetail;
    @BindView(R.id.linear_personal_activity_invoice_detail)
    LinearLayout mLinearPersonalActivityInvoiceDetail;
    @BindView(R.id.tv_name_activity_invoice_detail)
    TextView mTvNameActivityInvoiceDetail;
    @BindView(R.id.linear_name_activity_invoice_detail)
    LinearLayout mLinearNameActivityInvoiceDetail;
    @BindView(R.id.tv_tag_activity_invoice_detail)
    TextView mTvTagActivityInvoiceDetail;
    @BindView(R.id.linear_tag_activity_invoice_detail)
    LinearLayout mLinearTagActivityInvoiceDetail;
    @BindView(R.id.tv_address_activity_invoice_detail)
    TextView mTvAddressActivityInvoiceDetail;
    @BindView(R.id.linear_address_activity_invoice_detail)
    LinearLayout mLinearAddressActivityInvoiceDetail;
    @BindView(R.id.tv_phone_activity_invoice_detail)
    TextView mTvPhoneActivityInvoiceDetail;
    @BindView(R.id.linear_phone_activity_invoice_detail)
    LinearLayout mLinearPhoneActivityInvoiceDetail;
    @BindView(R.id.tv_bank_activity_invoice_detail)
    TextView mTvBankActivityInvoiceDetail;
    @BindView(R.id.linear_bank_activity_invoice_detail)
    LinearLayout mLinearBankActivityInvoiceDetail;
    @BindView(R.id.tv_bank_id_activity_invoice_detail)
    TextView mTvBankIdActivityInvoiceDetail;
    @BindView(R.id.linear_bank_id_activity_invoice_detail)
    LinearLayout mLinearBankIdActivityInvoiceDetail;
    @BindView(R.id.line_activity_invoice_detail)
    View mLine;

    private LoadingPagerHead mLoadingPagerHead;
    private int mOrderId;

    /**
     * 跳转订单发票详情界面
     *
     * @param orderId 订单Id
     */
    public static Intent call2InvoiceDetailActivity(Context context, int orderId) {
        Intent intent = new Intent(context, InvoiceDetailActivity.class);
        intent.putExtra(Contans.INTENT_DATA, orderId);
        return intent;
    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("发票详情");
        getInvoice();
    }

    @Override
    protected void initView() {
        super.initView();
        mOrderId = getIntent().getIntExtra(Contans.INTENT_DATA, -1);
    }

    @Override
    protected View getContentView(Context context) {
        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {
                onBackPressed();
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(InvoiceDetailActivity.this, R.layout.activity_invoice_detail);
            }

            @Override
            protected void reLoading() {
                getInvoice();
            }
        };
        return mLoadingPagerHead;
    }

    @Override
    protected void returnMainActivity(View v) {
        onBackPressed();
    }

    private void getInvoice() {
        mLoadingPagerHead.showPagerView(Contans.STATE_LOADING);
        Observable<StatusCode<OrderInvoiceBean>> observable = ApiUtils.getApi().getOrderInvoice(BaseApplication.getToken(), BaseApplication.getUserId(), mOrderId);
        HttpUtil.getInstance().toSubscribe(observable,
                new ProgressSubscriber<OrderInvoiceBean>(this) {
                    @Override
                    protected void _onNext(StatusCode<OrderInvoiceBean> statusCode) {
                        OrderInvoiceBean data = statusCode.getData();
                        if (data == null){
                            mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                            return;
                        }
                        mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                        initUI(data);
                    }

                    @Override
                    protected void _onError(String message) {
                        mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                    }
                }, "", lifecycleSubject, false, true);
    }

    private void initUI(OrderInvoiceBean data) {
        mTvOrdernoActivityInvoiceDetail.setText(data.getOrderNumber());
        mTvDateActivityInvoiceDetail.setText(data.getTime());
        if (3 == data.getTitle()) {
            mTvStatusActivityInvoiceDetail.setText("不开发票");
            mLinearTypeActivityInvoiceDetail.setVisibility(View.GONE);
            mLine.setVisibility(View.GONE);
        } else {
            mTvStatusActivityInvoiceDetail.setText("已开发票");
        }
        if (1 == data.getTitle()) {
            mTvTypeActivityInvoiceDetail.setText("纸质发票");
            mLinearTypeActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mLinearPersonalActivityInvoiceDetail.setVisibility(View.VISIBLE);
        }
        if (2 == data.getTitle() && 1 == data.getGenre()) {
            mTvTypeActivityInvoiceDetail.setText("普通发票");
            mLinearTypeActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvNameActivityInvoiceDetail.setText(data.getName());
            mLinearNameActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvTagActivityInvoiceDetail.setText(data.getNumber());
            mLinearTagActivityInvoiceDetail.setVisibility(View.VISIBLE);
        }
        if (2 == data.getTitle() && 2 == data.getGenre()) {
            mTvTypeActivityInvoiceDetail.setText("专用发票");
            mLinearTypeActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvNameActivityInvoiceDetail.setText(data.getName());
            mLinearNameActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvTagActivityInvoiceDetail.setText(data.getNumber());
            mLinearTagActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvAddressActivityInvoiceDetail.setText(data.getAddress());
            mLinearAddressActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvPhoneActivityInvoiceDetail.setText(data.getPhone());
            mLinearPhoneActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvBankActivityInvoiceDetail.setText(data.getBlank());
            mLinearBankActivityInvoiceDetail.setVisibility(View.VISIBLE);
            mTvBankIdActivityInvoiceDetail.setText(data.getAccount());
            mLinearBankIdActivityInvoiceDetail.setVisibility(View.VISIBLE);
        }
    }
}
